﻿if (typeof Admin == 'undefined')
    Admin = {};
Admin.Core = {
    Init: {
        Resize: function () {
            clearInterval(timeintarval);
            $(function () {
                $("#main").css({ 'width': $(window).width() - 1, 'height': $(window).height() - $('#divbottom').height() });
                $('#main').layout();
                $('#main').layout('panel', 'west').panel({
                    onCollapse: function () {
                        $("#main").css({ 'width': $(window).width() - 1, 'height': $(window).height() - $('#divbottom').height() });
                        $('#main').layout();
                        $('#tbproduct').datagrid('fitColumns');
                    },
                    onExpand: function () {
                        $("#main").css({ 'width': $(window).width() - 1, 'height': $(window).height() - $('#divbottom').height() });
                        $('#main').layout();
                        $('#tbproduct').datagrid('fitColumns');
                    }

                });

            });

            var timeintarval = setInterval("$('#tbproduct').datagrid('fitColumns')", 1000);
            Admin.Core.Processor.ResizeDataGrid('container', 'west');
        },
        Onload: function () {
            Admin.Core.Init.Resize();
            Admin.Core.Interface.SelectecMenu();
            $('#container .datagrid-wrap').addClass('fixGridInChrom');

        },
        LoadDataToGridProduct: function () {
            $(function () {
                $('#tbproduct').datagrid({
                    method: 'get',
                    singleSelect: true,
                    url: 'Products/Service/Product.ashx',
                    pagination: true,
                    rownumbers: true,
                    nowrap: false,
                    fitColumns: true,
                    showPageList: false,
                    pageSize: 50,
                    onLoadError: function () {
                        $.messager.alert('Thông báo!', 'Load dữ liệu bị lỗi!', 'error');

                        return;
                    },
                    onLoadSuccess: function () {


                    },
                    onClickRow: function (id, ob) {

                    }
                });
            });
        }

    },
    Processor: {

        OpenWindow: function (type, url, div, title, width, height, mode, coll, min, max, close, resize, drag, icon, idchild) {

            if (type == 1) {

                $('#' + div).window({ //window
                    href: url,
                    title: title,
                    width: width,
                    height: height,
                    modal: mode,
                    collapsible: coll,
                    minimizable: min,
                    maximizable: max,
                    closable: close,
                    resizable: resize,
                    draggable: drag,
                    cache: false,
                    iconCls: icon,
                    onClose: function () {

                        flagAddPositionInputInfo = false;
                        flagUpdateNhapThongTin = false;
                    },
                    onLoad: function () {

                    },
                    onMaximize: function () {
                        switch (idchild) {
                            case 1:
                                {
                                    Admin.Core.Processor.ResizeWindow('quanly', 'dataGrid', delta);
                                    break;
                                }
                        }
                    },
                    onRestore: function () {
                        switch (idchild) {
                            case 1:
                                {
                                    Admin.Core.Processor.ResizeWindow('quanly', 'dataGrid', delta);
                                    break;
                                }
                        }
                    }

                });
            }
            else if (type == 2) { //dialog

                $('#' + div).dialog({
                    href: url,
                    title: title,
                    width: width,
                    height: height,
                    modal: mode,
                    collapsible: coll,
                    minimizable: min,
                    maximizable: max,
                    closable: close,
                    resizable: resize,
                    draggable: drag,
                    cache: false,
                    iconCls: icon,
                    onClose: function () {

                        flagAddTab = false;
                    }
                });
            }

        },
        CloseWindow: function (type, div) {

            type == 1 ? $('#' + div).window('close') : $('#' + div).dialog('close');
            // type == 1 ? $('#' + div).window('destroy') : $('#' + div).dialog('destroy');

        },
        LogOut: function (id) {
            //var session = $('#MainContent_hdSession').val();
            
            //id != 'logout' || id.toString() != 'refresh'
            if (id == 'logout') {
                Admin.Core.Browser.setCookie('logout', '', 1);
            }
            
            $.ajax({
                'type': 'POST',
                'url': 'Service/AdminService.asmx/LogOut',
                dataType: "json",
                //data: { 'param': session, id: id.id },
                async: true,
                success: function (msg) {
                    if (msg.result == true) {

                        window.location.href = 'Login.aspx';
                    }
                }
            });

            window.location.href = 'Login.aspx';
        },
        Login: function () {
            var username = $('#txtUserName').val();
            var password = $('#txtPassword').val();

            $.ajax({
                'type': 'POST',
                //'url': 'Service/Login.ashx',
                url: 'Service/AdminService.asmx/Login',
                data: { 'User': username, 'Pass': password },
                dataType: "json",
                async: true,
                success: function (msg) {
                    try {
                        if (msg.result == true) {
                            Admin.Core.Browser.setCookie('logout', username, 1);
                            window.location.href = 'Default.aspx';
                        }
                        else {

                            $('#info').html('Người dùng hoặc mã số không đúng!');
                            $('#txtUserName').val('');
                            $('#txtPassword').val('');
                            $('#txtUserName').focus();
                            return;
                        }
                    }
                    catch (e) {
                        window.location.href = 'Default.aspx';
                    }

                }
                /*error: function (XMLHttpRequest, textStatus, errorThrown) {
                $('#info').html('Người dùng hoặc mã số không đúng!');
                return;
                }*/
            });
        },
        OnKeyPressLogin: function (event) {
            if (event.keyCode == 13) {
                Admin.Core.Processor.Login();
            }
            else {

                return;
            }


        },
        ResizeDataGrid: function (hcontainer, wcontainer) //hcontainer : tab div chua grid, wcontainer: lay vi tri offset cho grid
        {
            var h = $('#' + hcontainer).height();
            var w = $('#' + wcontainer).width();
            $('#' + hcontainer + ' .datagrid-wrap').height(h); // set lai chieu cao cho gridview
            $('#' + hcontainer + '.datagrid-pager').offset({ top: $(window).height() - 20, left: w }); //set vi tri cho paging
        }


    },
    Interface: {
        SelectecMenu: function () {
            $('.MenuLeftTool li').click(function () {
                $('.MenuLeftTool li a').removeClass();
                $(this).children().addClass('selected');
            });
        },
        LoadIconAndLogo: function () {
            //
            $('.logo').css('background', 'url(images/logo/logo_SaiGonTrack.png) no-repeat 10px 5px');
            var v_css = document.createElement("link");
            v_css.rel = "shortcut icon";
            v_css.href = 'images/icon/VietbandoTrackingClient.ico';
            document.getElementsByTagName('head')[0].appendChild(v_css);
        }
    },
    Browser: {
        setCookie: function (c_name, value, exdays) {
            var exdate = new Date();
            exdate.setDate(exdate.getDate() + exdays);
            var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
            document.cookie = c_name + "=" + c_value;
        },
        getCookie: function (c_name) {
            var i, x, y, ARRcookies = document.cookie.split(";");
            for (i = 0; i < ARRcookies.length; i++) {
                x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
                y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
                x = x.replace(/^\s+|\s+$/g, "");
                if (x == c_name) {
                    return unescape(y);
                }
            }
        }
    },

    Ajax: {
        CallServer: function (pUrl, pData, pFuncNameSuccess, pType, pAsync, pdataType, pcontenType) {
            pType = pType;
            pAsync = pAsync;

            $.ajax({
                url: pUrl,
                async: pAsync,
                cache: false,
                type: pType,
                data: pData,
                contentType: pcontenType,
                dataType: pdataType,
                success: pFuncNameSuccess,
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    $.messager.alert('Thông báo!', 'Load thiết bị bị lỗi!', 'error');
                    clearTimeout(timeoutLoadVehicleMK);
                    return;
                }
            });
        },
        LoadDataToGrid: function (url, div, pData) {

            $(document).ready(function () {
                $.post(url + '?para=' + pData,
            function (data) {
                $("div#" + div).empty().html('<img src="Images/5.gif"/>');
                $("div#" + div).html(data.replace('name="__VIEWSTATE" id="__VIEWSTATE"', 'id="cuibap" '));
                Admin.Core.Processor.ResizeWindow('quanly', div, delta);

            });
            });


        }
    }

}